/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.digest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.GeneralSecurityException;
import java.security.Key;

/**
 * DES加解密
 *
 * @author Mr.css
 * @version 2020-07-28 15:49
 */
public class DES {
    private DES() {
    }

    private static final String CIPHER_DES = "DES";
    private static final String CIPHER_DES_ECB_PKCS5Padding = "DES/ECB/PKCS5Padding";

    /**
     * 生成密钥
     *
     * @return SecretKey
     * @throws GeneralSecurityException -
     */
    public static SecretKey generateSecretKey(byte[] key) throws GeneralSecurityException {
        DESKeySpec dks = new DESKeySpec(key);
        return SecretKeyFactory.getInstance(CIPHER_DES).generateSecret(dks);
    }

    /**
     * 数据加密
     *
     * @param src 源
     * @param key 密钥
     * @return 加密结果
     * @throws GeneralSecurityException -
     */
    public static byte[] encrypt(byte[] src, Key key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(CIPHER_DES_ECB_PKCS5Padding);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(src);
    }

    /**
     * 数据解密
     *
     * @param src 源
     * @param key 密钥
     * @return 解密结果
     * @throws GeneralSecurityException -
     */
    public static byte[] decrypt(byte[] src, Key key) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance(CIPHER_DES_ECB_PKCS5Padding);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(src);
    }

    /**
     * 十六进制加密
     *
     * @param data 数据
     * @param key  密钥
     * @return 加密数据
     * @throws GeneralSecurityException -
     */
    public static String encryptHex(String data, Key key) throws GeneralSecurityException {
        return Hex.encodeToString(encrypt(data.getBytes(), key));
    }

    /**
     * 十六进制解密
     *
     * @param encryptedData 数据
     * @param key           密钥
     * @return 解密数据
     * @throws GeneralSecurityException -
     */
    public static String decryptHex(String encryptedData, Key key) throws GeneralSecurityException {
        return new String(decrypt(Hex.decode(encryptedData.toCharArray()), key));
    }
}
